class PostTests extends TestHelper {

  def post

  protected void setUp() {
    super.setUp();
    post = new Post(
        title: "foo",
        teaser: "bar",
        content: "foo bar",
        published: false)
  }

//  void testCrud() {
//
////    post = new Post(title: "First Grails Project",
////        teaser: "Clearing out the clutter",
////        content: "The full content of the article",
////        published: false)
//
//    assertNotNull(post.save())
//
//    def id = post.id
//    println("Got id: ${id}")
//    def retrievedPost = Post.get(id)
//
//    assertEquals("First Grails Project", retrievedPost.title)
//
//    assertNull(Post.get(10));
//
//    post.published = true
//    post.save()
//
//    retrievedPost = Post.get(id)
//    assertEquals(true, retrievedPost.published)
//
//    post.delete()
//    assertEquals(null, Post.get(id))
//  }

  void testTitleCannotBeNull() {
    fieldCannotBeNull(post, "title")
  }

  void testTitleCannotBeEmpty() {
    fieldCannotBeEmpty(post, "title")
  }

  void testTitleMustBeLessThan51Characters() {
    fieldLengthInvalid(post, "title", 51)
  }

  void testTitleCanBe50Characters() {
    fieldLengthValid(post, "title", 50)
  }

  void testContentCannotBeNull() {
    fieldCannotBeNull(post, "content")
  }

  void testContentCannotBeEmpty() {
    fieldCannotBeEmpty(post, "content")
  }

  void testTeaserMustBeLessThan256Characters() {
    fieldLengthInvalid(post, "teaser", 256)
  }

  void testTeaserCanBe255Characters() {
    fieldLengthValid(post, "teaser", 255)
  }

  void testTeaserCanBeNull() {
    fieldCanBeNull(post, "teaser")
  }

  void testTeaserCanBeEmpty() {
    fieldCanBeEmpty(post, "teaser")
  }

  void testPublishedCannotBeNull() {
    fieldCannotBeNull(post, "published")
  }

  void testAPostCanHaveMultipleTags() {
    post.addToTags(new Tag(name: "code"))
    post.addToTags(new Tag(name: "organisation"))
    assertNotNull(post.save())

    assertEquals(2, post.tags.size())
    assertNotNull(Tag.findByName("code"))
    assertNotNull(Tag.findByName("organisation"))
  }

  void testTagsAreReusedNotDuplicatedWhenAssociatedWithAPost() {
    def codeTag = new Tag(name:"code")
    def testTag = new Tag(name:"testing")
    def orgTag = new Tag(name:"organisation")
    assertEquals(0, Tag.list().size())
    def secondPost = new Post(
        title: "2nd Post",
        teaser: "bar",
        content: "foo bar",
        published: false)
        .addToTags(codeTag)
        .addToTags(testTag).save()

    post.addToTags(codeTag)
      .addToTags(orgTag).save()

    assertEquals(3, Tag.list().size())
  }

//  void testTagsAreRetrievableInTheOrderTheyAreAdded() {
//    post.addToTags(new Tag(name: "a"))
//    post.addToTags(new Tag(name: "b"))
//    post.addToTags(new Tag(name: "c"))
//    post.addToTags(new Tag(name: "d"))
//    post.addToTags(new Tag(name: "e"))
//    post.save()
//    assertEquals("a", post.tags[0].name)
//    assertEquals("b", post.tags[1].name)
//    assertEquals("c", post.tags[2].name)
//    assertEquals("d", post.tags[3].name)
//    assertEquals("e", post.tags[4].name)
//  }


  void testTagsAreRetrievedInAlphabeticalOrderAsAStringForDisplay() {
    post.addToTags(new Tag(name:"foo")).addToTags(new Tag(name:"bar"))
    assertEquals("bar foo", post.tagsAsString)
  }

  void testCanDetermineIfATagIsAlreadyInAPost() {
    post.addToTags(new Tag(name:"monkey"))
    assertTrue(post.hasTag("monkey"))
    assertFalse(post.hasTag("somethingelse"))
  }

  void testWeCanAddATagByName() {
    post.addTag("bar")
    assertTrue(post.hasTag("bar"))
  }

  void testAddingATagByNameDoesNotCreateANewTagIfTheTagExistsElsewhere() {
    new Tag(name:"monkey").save()
    post.addTag("monkey")
    assertEquals(1, Tag.findAllByName("monkey").size())
  }

  void testAddComment() {
    post.addToComments(
        new Comment(
        comment: "a comment",
        dateCreated: new Date(),
        post: post,
        who: new Commentator(name: "Me"))).save()

    assertNotNull(Comment.findByComment("a comment"))
    assertNotNull(Post.findByTitle("foo"))
  }
}
